package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.annotation.ParamParser;
import com.framework.model.ApiResult;
import com.framework.util.WebUtils;
import com.sihan.framework.etm.dto.PermissionAddDTO;
import com.sihan.framework.etm.dto.PermissionUpdateDTO;
import com.sihan.framework.etm.entity.Permission;
import com.sihan.framework.etm.service.PermissionService;
import com.sihan.framework.etm.vo.PermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import javax.validation.Valid;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "权限管理")
@RestController
@RequestMapping("/system/v0/permission")
public class AdminPermissionController {

  @Autowired
  private PermissionService permissionService;

  @ApiOperation("查询用户的所有权限的菜单权限")
  @GetMapping("/menu")
  @RequiresAuthentication
  public ApiResult<?> menuPermissions() {
    List<PermissionVO> result = permissionService.selectMenuPermissions(WebUtils.username());
    return ApiResult.success(result);
  }

  @ApiOperation(value = "获得所有权限(数组形式)")
  @GetMapping("/list")
  @BusinessLog(value = "获得权限信息(数组形式)")
  @RequiresAuthentication
  public ApiResult<?> permissionList() {
    List<PermissionVO> result = permissionService.permissionList();
    return ApiResult.success(result);
  }

  @ApiOperation(value = "获得所有权限(树状形式)")
  @GetMapping("/tree")
  @BusinessLog(value = "获得权限信息(树状形式)")
  @RequiresAuthentication
  public ApiResult<?> permissionTree() {
    List<PermissionVO> result = permissionService.permissionTree();
    return ApiResult.success(result);
  }
  
  @ApiOperation(value = "获得角色所有权限")
  @GetMapping("/role/all")
  @BusinessLog(value = "获得角色所有权限")
  @RequiresAuthentication
  public ApiResult<?> rolePermissionTree(String roleId) {
    return ApiResult.success(permissionService.rolePermissions(roleId));
  }

  @ApiOperation(value = "添加权限")
  @PostMapping("/add")
  @BusinessLog(value = "添加权限信息")
  public ApiResult<?> addPermission(@Valid @ApiParam(name = "dto", value = "权限数据", required = true) @RequestBody PermissionAddDTO dto) {
    Permission permission = new Permission();
    BeanUtils.copyProperties(dto, permission);
    permissionService.addPermission(permission);
    return ApiResult.success();
  }

  @ApiOperation("更新权限")
  @PostMapping("/update")
  @BusinessLog(value = "更新权限")
  public ApiResult<?> updatePermission(@Valid @ApiParam(name = "dto", value = "权限数据", required = true) @RequestBody PermissionUpdateDTO dto) {
    Permission permission = new Permission();
    BeanUtils.copyProperties(dto, permission);
    permissionService.updatePermission(permission);
    return ApiResult.success();
  }

  @ApiOperation("删除权限")
  @PostMapping("/delete")
  @BusinessLog(value = "删除权限")
  public ApiResult<?> deletePermission(@Valid @RequestBody @ParamParser List<String> permissionIds) {
    permissionService.deletePermission(permissionIds);
    return ApiResult.success();
  }
}
